function validateRegisterForm(form) {
    var errorList = new Array();
    
    var fromRegister = $("#edit-id", form).val() == 0;

    var username = form['username'].value;
    var password = form['password'].value;
    var repassword = form['repassword'].value;
    var name = form['name'].value;
    var email = form['email'].value;
    var reemail = form['reemail'].value;
    var src = form['image'].value;

    var validatePassword = fromRegister;
    var validateEmail = fromRegister;
    var emailchanged = !fromRegister &&  $("#edit-oldemail", form).val() != email;

    if(!fromRegister){
        if(password){
            //ValidatePassword
            password = form['newpassword'].value;
            validatePassword = true;
        }
        if(emailchanged || ($("#edit-reemail").val() != '' && $("#edit-reemail").val() != email)){
            validateEmail = true;
        }
    }

    if (username == null || username == "") {
        errorList.push('Empty username');
        fieldHighlight(form['username'], true);
    }
    if(validatePassword){
        var expPass = /(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,12})$/;
        if (password == null || password == "") {
            if(fromRegister)
                errorList.push('Empty password');
            else
                errorList.push('Empty new password');
                
            fieldHighlight(fromRegister? form['password']: form['newpassword'], true);
        } else if (!expPass.test(password)) {
            if(fromRegister)
                errorList.push('Password is not safe enough. Length must be between 6 and 12 characters and at least one digit and one alfanumeric character are necessary. Special caracters are not allowed');
            else
                errorList.push('New password is not safe enough. Length must be between 6 and 12 characters and at least one digit and one alfanumeric character are necessary. Special caracters are not allowed');
            fieldHighlight(fromRegister? form['password']: form['newpassword'], true);
        } else if (password != repassword) {
            errorList.push('Password and repeated password have differences, please repeat again');
            fieldHighlight(fromRegister? form['password']: form['newpassword'], true);
            fieldHighlight(form['repassword'], true);
        }
    }

    if (name == null || name == "") {
        errorList.push('Empty name');
        fieldHighlight(form['name'], true);
    }
    if(validateEmail){
        var expEmail = /[\w-\.]{3,}@([\w-]{2,}\.)*([\w-]{2,}\.)[\w-]{2,4}/;
        if (email == null || email == "") {
            errorList.push('Empty email');
            fieldHighlight(form['email'], true);
        } else if (!expEmail.test(email)) {
            errorList.push('Email is incorrect');
            fieldHighlight(form['email'], true);
        } else if (email != reemail) {
            errorList.push('Email and repeated email have differences, please repeat again');
            fieldHighlight(form['email'], true);
            fieldHighlight(form['reemail'], true);
        }
    }

    if (src != "") {
        var format = src.substring(src.length - 3);
        if (format != 'jpg' && format != 'png' && format != 'gif' && format != 'jpeg' && format != 'bmp') {
            errorList.push('Only jpeg, gif, bmp and png images are accepted for upload');
            fieldHighlight(form['image'], true);
        }
    }

    if (errorList.length > 0) {
        showErrorList(errorList);
        return false;
    } else {
        return true;
    }
}

function validateImage(form){
    var src = form['image'].value;
    var format = src != "" ? src.substring(src.length - 3) : '';
    if (format != 'jpg' && format != 'png' && format != 'gif' && format != 'jpeg' && format != 'bmp') {
        showError('Only jpeg, gif, bmp and png images are accepted for upload');
        fieldHighlight(form['image'], true);
		return false;
    }
	return true;
}

function loadImage() {
    var form = document.forms["form-edit"];
	if(validateImage(form)){
		fieldHighlight(form['image'], false);
		var options = {
			dataType: 'json', 
			success: function(data){
				$("#preview").attr("src", data.image);
			},
			url: $(form).attr("action") +"?preview=1"     
		};
		$(form).ajaxSubmit(options);
	}
}